package com.xuyiwei.xblog.modular.article.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.xuyiwei.xblog.business.article.result.article.out.ArticleDto;
import com.xuyiwei.xblog.business.article.result.article.input.ArticleConditionVO;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.common.constant.factory.PageFactory;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.page.PageInfoBT;
import com.xuyiwei.xblog.core.util.ToolUtil;
import com.xuyiwei.xblog.business.article.model.Article;
import com.xuyiwei.xblog.business.article.service.IArticleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.xuyiwei.xblog.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文章管理控制器
 *
 * @author fengshuonan
 * @Date 2018-10-22 22:32:52
 */
@Controller
@RequestMapping("/article/article")
public class ArticleController extends BaseController {

    private String PREFIX = "/article/article/";

    @Autowired
    private IArticleService articleService;

    /**
     * 跳转到文章管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "article.html";
    }

    /**
     * 跳转到添加文章管理
     */
    @RequestMapping("/article_add")
    public String articleAdd() {
        return PREFIX + "article_add.html";
    }

    /**
     * 跳转到markdown编辑器页面
     */
    @RequestMapping("/article_publish_md")
    public String articlePublishMd(String id) {
        this.setAttr("id",id);
        return PREFIX + "article_publish_md.html";
    }

    /**
     *
     * 跳转到html编辑器页面
     */
    @RequestMapping("/article_publish_h5")
    public String articlePublishH5(String id) {
        this.setAttr("id",id);
        return PREFIX + "article_publish_h5.html";
    }

    /**
     *
     * 跳转到编辑器页面
     */
    @RequestMapping("/article_publish")
    public String article_publish(String id,String is_markdown){
        this.setAttr("id",id);
        if(is_markdown!=null && !is_markdown.equals("undefined") && is_markdown.equals("true")){
            return PREFIX + "article_publish_md.html";
        }else{
            return PREFIX + "article_publish_h5.html";
        }
    }

    /**
     * 跳转到修改文章管理
     */
    @RequestMapping("/article_update/{articleId}")
    public String articleUpdate(@PathVariable Integer articleId, Model model) {
        Article article = articleService.selectById(articleId);
        model.addAttribute("item",article);
        LogObjectHolder.me().set(article);
        return PREFIX + "article_edit.html";
    }

    /**
     * 获取文章管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Result<PageInfoBT<ArticleDto>> list(ArticleConditionVO vo) {
        Result<PageInfoBT<ArticleDto>> result = Result.instance();
        try{
            Page<ArticleDto> page = new PageFactory<ArticleDto>().defaultPage();
            List<ArticleDto> articles = articleService.findPageByCondition(page,vo,page.getOrderByField(),page.isAsc());
            result.setData(this.packForBT(page.setRecords(articles)));
        }catch (Exception e){
            result.setError("500","获取文章管理列表出错");
        }
        return result;
    }

    /**
     * 新增文章管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Result<Boolean> add(Article article, @RequestParam(value = "tags", required=false) Long[] tags,MultipartFile file ) {
        Result<Boolean> result = Result.instance();
        try{
            articleService.publish(article, tags,file );
        }catch (GunsException e){
            result.setError("500",e.getMessage());
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 删除文章管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(required = true, value = "ids[]") List<Long> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.articleService.deleteBatchIds(ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 修改文章管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Result<Boolean> update(Article article) {
        Result<Boolean> result = Result.instance();
        try{
            articleService.updateById(article);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 文章管理详情
     */
    @RequestMapping(value = "/detail/{articleId}")
    @ResponseBody
    public Result<ArticleDto> detail(@PathVariable("articleId") Long articleId) {
        Result<ArticleDto> result = Result.instance();
        try{
            ArticleDto article = articleService.selectArticleById(articleId);
            result.setData(article);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }


    /**
     * 获取图片素材库
     */
    @RequestMapping(value = "/material")
    @ResponseBody
    public Result<List<String>> material(){
        Result<List<String>> result = Result.instance();
        try{
            List<String> list = articleService.listMaterial();;
            result.setData(list);
        }catch (Exception e){
            result.setError("500","获取图片库出错");
        }
        return result;
    }

    /**
     * 文章置顶  顺便做了个ajax 数据序列化的小测试
     */
    @RequestMapping(value = "/top")
    @ResponseBody
    public Result<Boolean> top(Boolean top,@RequestParam(required = true, value = "ids") List<Long> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.articleService.top(top,ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 文章置顶
     */
    @RequestMapping(value = "/recommend")
    @ResponseBody
    public Result<Boolean> recommend(Boolean recommend,@RequestParam(required = true, value = "ids") List<Long> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.articleService.recommend(recommend,ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }

    /**
     * 发布文章
     */
    @RequestMapping(value = "/punishArticle")
    @ResponseBody
    public Result<Boolean> punishArticle(@RequestParam(required = true, value = "ids") List<Long> ids) {
        Result<Boolean> result = Result.instance();
        try{
            if (ToolUtil.isEmpty(ids)) {
                throw new GunsException(BizExceptionEnum.REQUEST_NULL);
            }
            this.articleService.punishArticle(ids);
        }catch (Exception e){
            result.setError(BizExceptionEnum.REQUEST_NULL);
        }
        return result;
    }
}
